package edu.suda.LoopCodePlus.service.impl;

import edu.suda.LoopCodePlus.entity.User;
import edu.suda.LoopCodePlus.entity.UserExtra;
import edu.suda.LoopCodePlus.mapper.UserExtraMapper;
import edu.suda.LoopCodePlus.mapper.UserMapper;
import edu.suda.LoopCodePlus.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserExtraMapper userExtraMapper;
    @Override
    public boolean save(User user) {
        try {
            userMapper.insert_regUser(user);
            User user1 = userMapper.select_userByEmail(user.getRegMail());
            System.out.println("用户插入成功");
            UserExtra userExtra = UserExtra.builder()
                    .userId(user1.getUserId())
                    .mulPoint(0)
                    .sinPoint(0)
                    .weekPoint(0)
                    .build();
            userExtraMapper.insert_userExtra(userExtra);
            System.out.println("用户额外信息插入成功");
            return true;
        }catch (Exception e){
            return false;
        }
    }
    @Override
    public User findUserById(Integer userId) {
        User user = userMapper.select_userById(userId);
        return user;
    }
    @Override
    public User findUserByRegMail(String email) {
        User user = userMapper.select_userByEmail(email);
        return user;
    }
    @Override
    public UserExtra findUserExtraById(Integer userId) {
        UserExtra userExtra;
        try {
            userExtra = userExtraMapper.select_userExtraById(userId);
            return userExtra;
        }catch (Exception e){
            System.out.println(e.getMessage());
            return null;
        }
    }
    @Override
    public boolean updateUserExtraById(Integer userId, UserExtra userExtra) {
        try {
            userExtraMapper.update_userExtraById(userId,userExtra);
            return true;
        }catch (Exception e){
            System.out.println(e.getMessage());
            return false;
        }
    }
    @Override
    public boolean updateUserRoleById(Integer userId, User user) {
        try {
            userMapper.update_userRole(user.getRole(),userId);
            return true;
        }catch (Exception e){
            System.out.println(e.getMessage());
            return false;
        }
    }
}
